package com.fan.base.util;


import android.util.TypedValue;

import androidx.core.content.ContextCompat;

/**
 * 资源工具类
 *
 * @author fan
 * @date Created in 2019/1/28
 */

public class ResourceUtil {

    private static Object rp;


    /**
     * 获取颜色值
     *
     * @param color R.color.xxx
     * @return int
     */
    public static int color(int color) {
        return ContextCompat.getColor(Tools.getContext(), color);
    }

    /**
     * 获取string.xml值
     *
     * @param res R.string.xxx
     * @return int
     */
    public static String getString(int res) {
        return Tools.getContext().getResources().getString(res);
    }


    /**
     * dip-->px
     */
    public static int dip2Px(int dip) {
        // px/dip = density;
        // density = dpi/160
        // 320*480 density = 1 1px = 1dp
        // 1280*720 density = 2 2px = 1dp

        float density = Tools.getContext().getResources().getDisplayMetrics().density;
//        int px = (int) (dip * density + 0.5f);
        return (int) (dip * density + 0.5f);
    }

    /**
     * px-->dip
     */
    public static int px2dip(int px) {

        float density = Tools.getContext().getResources().getDisplayMetrics().density;
//        int dip = (int) (px / density + 0.5f);
        return (int) (px / density + 0.5f);
    }

    /**
     * sp-->px
     */
    public static int sp2px(int sp) {
        return (int) (TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_SP, sp, Tools.getContext().getResources().getDisplayMetrics()) + 0.5f);
    }
}
